﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using HuaProject.DBUtility;
using System.Web.Security;
using System.Web;
using Wuqi.Webdiyer;
using System.Data;//Please add references

namespace HuaProject.DAL
{
    public class Tool
    {
        /// <summary>
        /// 过滤特殊字符串
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public static string UrnHtml(string strHtml)
        {
            string[] aryReg = { "'", "<", ">", "%", "\"\"", ",", ".", ">=", "=<", "-", "_", ";", "||", "[", "]", "&", "/", "-", "|", " ", };
            for (int i = 0; i < aryReg.Length; i++)
            {
                strHtml = strHtml.Trim().Replace(aryReg[i], string.Empty);
            }
            return strHtml;
        }
        /// <summary>
        /// 分页数据查询
        /// </summary>
        /// <param name="Tables">表名</param>
        /// <param name="PrimaryKey">主键</param>
        /// <param name="Sort">排序语句</param>
        /// <param name="Filter">条件</param>
        /// <param name="anp">分页控件</param>
        /// <returns></returns>
        public DataSet GetPageData(string Tables, string PrimaryKey, string Sort, string Filter, AspNetPager anp)
        {
            anp.RecordCount = Convert.ToInt32(GetNo(Tables, Filter));
            return GetPagination(Tables, PrimaryKey, Sort, anp.CurrentPageIndex, anp.PageSize, Filter);
        }
        /// <summary>
        /// 获得总条数
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="str">查询条件</param>
        /// <returns>总条数</returns>
        public long GetNo(string table, string str)
        {
            try
            {
                string sql = "select count(*) from " + table + " where  ";
                sql += str;
                return Convert.ToInt64(DbHelperSQL.GetSingle(sql));
            }
            catch
            {
                return 0;
            }

        }
        /// <param name="Tables">表名称</param>
        /// <param name="PrimaryKey">主关键字</param>
        /// <param name="Sort">排序语句</param>
        /// <param name="CurrentPage">当前页码</param>
        /// <param name="PageSize">分页尺寸</param>
        /// <param name="Fields">字段名</param>
        /// <returns></returns>
        public DataSet GetPagination(string Tables, string PrimaryKey, string Sort, int CurrentPage, int PageSize, string Filter)
        {
            string sql = "[SP_Pagination]";
            SqlParameter[] par = new SqlParameter[] {
                new SqlParameter("@Tables",SqlDbType.NChar),
                new SqlParameter("@PrimaryKey",SqlDbType.NChar),
                new SqlParameter("@Sort",SqlDbType.NChar),
                new SqlParameter("@CurrentPage",SqlDbType.Int),
                new SqlParameter("@PageSize",SqlDbType.Int),
                new SqlParameter("@Filter",SqlDbType.NChar),
            };
            par[0].Value = Tables;
            par[1].Value = PrimaryKey;
            par[2].Value = Sort;
            par[3].Value = CurrentPage;
            par[4].Value = PageSize;
            par[5].Value = Filter;
            return DbHelperSQL.RunProcedures(sql, par);
        }
    }
}
